//5.四舍五入保留2位小数（不够位数，则用0替补）  
        function keepTwoDecimalFull(num) {  
             var result = parseFloat(num);  
             if (isNaN(result)) {  
                 alert('传递参数错误，请检查！');  
                 return false;  
             }  
             result = Math.round(num * 100) / 100;  
             var s_x = result.toString(); //将数字转换为字符串
             
             var pos_decimal = s_x.indexOf('.'); //小数点的索引值
             

             // 当整数时，pos_decimal=-1 自动补0  
             if (pos_decimal < 0) {  
                 pos_decimal = s_x.length;  
                 s_x += '.';  
             }

             // 当数字的长度< 小数点索引+2时，补0  
             while (s_x.length <= pos_decimal + 2) {  
                 s_x += '0';  
             }  
             return s_x;  
        }  
function hasClass(elem, cls) {
	  cls = cls || '';
	  if (cls.replace(/\s/g, '').length == 0) return false; //当cls没有参数时，返回false
	  return new RegExp(' ' + cls + ' ').test(' ' + elem.className + ' ');
	}
	 
	function addClass(ele, cls) {
	  if (!hasClass(ele, cls)) {
	    ele.className = ele.className == '' ? cls : ele.className + ' ' + cls;
	  }
	}
	 
	function removeClass(ele, cls) {
	  if (hasClass(ele, cls)) {
	    var newClass = ' ' + ele.className.replace(/[\t\r\n]/g, '') + ' ';
	    while (newClass.indexOf(' ' + cls + ' ') >= 0) {
	      newClass = newClass.replace(' ' + cls + ' ', ' ');
	    }
	    ele.className = newClass.replace(/^\s+|\s+$/g, '');
	  }
	}
		document.querySelector(".Calculation-button").onclick = function(){
					var reg = new RegExp(/^\d*\.?\d+$/);  //正数
					var posPattern = new RegExp(/^\d+$/); //正整数
                    var kg = document.getElementById("kg").value;
                    var age = document.getElementById("age").value;
                    var days = document.getElementById("days").value;
                    var alert_text = "";
					var wszl,days_duns;
                    if(!reg.test(kg))
                    {
                        alert_text += "主子体重填写错误（必须为正数）";
						
                    }
                    if(!reg.test(age))
                    {
						alert_text += "\r主子年龄填写错误（必须为正数）";
                    }
                    if(!posPattern.test(days))
                    {
						alert_text += "\r制作周期填写错误（必须为正整数）";
                    }
                    if(alert_text != "")
                    {
                        alert(alert_text);
						return;
                    }
					if(age <1.5)
					{
						alert('奶猫请喂食母乳或羊奶，大于1.5个月后才可食用自制猫饭');
						return;
					}else if(age <= 4)
					{
						wszl = kg * 0.1*1000;
					}else if(age <=12)
					{
						wszl = kg * 0.07*1000;
					}else
					{
						wszl = kg * 0.04*1000;
					}
					if(age >= 1.5)
					{
						if(age < 4 )
						{
							days_duns = "3~4";
						}else if(age <= 12)
						{
							days_duns = " 3";
						}
						else
						{
							days_duns = "2~3";
						}
					}
					var sczl = wszl*days;
					var jdt = keepTwoDecimalFull(sczl*22/100);
					var jx = keepTwoDecimalFull(sczl*15/100);
					var egg = keepTwoDecimalFull(sczl*9/100);
					var jg = keepTwoDecimalFull(sczl*4.3/100);
					var zr = keepTwoDecimalFull(sczl*15.6/100);
					var ng = keepTwoDecimalFull(sczl*4/100);
					var nx = keepTwoDecimalFull(sczl*16/100);
					var fish = keepTwoDecimalFull(sczl*10.8/100);
					var food = keepTwoDecimalFull(sczl*3.6/100);
					var calcium = keepTwoDecimalFull(sczl*0.6/100);
					var KI = Math.round(sczl*0.75/5/100);
					var VitaminE = Math.round(sczl*0.75/5/100);
				addClass(document.querySelector(".Calculation"), "active")
				setTimeout(function(){
					addClass(document.querySelector(".sk-rotating-plane"), "active")
					document.querySelector(".Calculation").style.display = "none"
				},800)
				setTimeout(function(){
					removeClass(document.querySelector(".Calculation"), "active")
					removeClass(document.querySelector(".sk-rotating-plane"), "active")
					document.querySelector(".Calculation").style.display = "block"
					var div=document.createElement("div");
					div.id = "Result";
					div.className = "Calculation";
					div.innerHTML = '<div class="Result-div">\
					<p class="Result-p-h" align="left">喂食量</p>\
					<p class="Result-p-text" align="left">'+days+'天材料总量 ≈ '+sczl+' 克</p>\
					<p class="Result-p-text" align="left">主子一天吃 ≈ '+wszl+' 克</p>\
					<p class="Result-p-text" align="left">主子一天吃 ≈ '+days_duns+' 顿</p>\
					<p class="Result-p-h" align="left">所需材料</p>\
					<table class="Result-table">\
					<tr class="Result-table-tr"><th align="right" width="120px">原材料</th><th width="80px"> 原料比例</th><th width="60px"> 食材量</th><th width="40px"> 单位</th></tr>\
					<tr><td class="Result-table-td">鸡大腿[去皮骨]</td><td>22.00% ≈</td><td>'+jdt+'</td><td>克</td></tr>\
					<tr><td class="Result-table-td">鸡心</td><td>15.00% ≈</td><td>'+jx+'</td><td>克</td></tr>\
					<tr><td class="Result-table-td">鸡蛋[含蛋清]</td><td>9.00% ≈</td><td>'+egg+'</td><td>克</td></tr>\
					<tr><td class="Result-table-td">鸡肝</td><td>4.30% ≈</td><td>'+jg+'</td><td>克</td></tr>\
					<tr><td class="Result-table-td">猪瘦肉 | 里脊</td><td>15.60% ≈</td><td>'+zr+'</td><td>克</td></tr>\
					<tr><td class="Result-table-td">牛肝</td><td>4.00% ≈</td><td>'+ng+'</td><td>克</td></tr>\
					<tr><td class="Result-table-td">牛心</td><td>16.00% ≈</td><td>'+nx+'</td><td>克</td></tr>\
					<tr><td class="Result-table-td">青占鱼 | 秋刀鱼<br>马鲛鱼 | 三文鱼</td><td>10.80% ≈</td><td>'+fish+'</td><td>克</td></tr>\
					<tr><td class="Result-table-td">青口贝 | 淡菜</td><td>3.60% ≈</td><td>'+food+'</td><td>克</td></tr>\
					</table>\
					<p class="Result-p-h" align="left">营养剂</p>\
					<table class="Result-table">\
					<tr class="Result-table-tr"><th width="120px">营养剂</th><th width="80px">每克含量</th><th width="60px">添加剂量</th><th width="40px">单位</th></tr>\
					<tr><td class="Result-table-td">碳酸钙粉(克)</td><td>0.60% ≈</td><td>'+calcium+'</td><td>克</td></tr>\
					<tr><td class="Result-table-td">Nowfoods碘化钾<br>[225mcg/粒]</td><td>0.15% ≈</td><td>'+KI+'</td><td>粒</td></tr>\
					<tr><td class="Result-table-td">维生素E软胶囊<br>[100mg/粒]</td><td>0.15% ≈</td><td>'+VitaminE+'</td><td>粒</td></tr>\
					</table>\
					<br><hr><p align="right">以上信息四舍五入仅供参考</p>\
					</div>\
					<div class="back-button">返回</div>';
					document.body.appendChild(div);
					document.querySelector(".back-button").onclick = function(){
						var x=document.getElementById("Result")
						x.remove(x.selectedIndex)
					}
				},1500)
		}